package com.bases {
	import com.Event.ImageDataEvent;
	import com.Interface.IBase;
	import com.debug.TDebug;
	import com.windows.AlertWindow;
	import config.TConfig;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;

	public class TImage extends TUIComponent implements IBase {
		protected var bitmap:Bitmap;
		private var loader:Loader;
		//790,556
		protected var bitmapWidth:Number = TConfig.getInstance().IMAGE_WIDTH;
		protected var bitmapHeight:Number = TConfig.getInstance().IMAGE_HEIGHT;
		public var needDispatch:Boolean = false;
		//是否需要圆角化
		public var corner:Boolean = false;
		//是否需要无损缩放大小
		private var _needScale:Boolean = false;
		/*用于检测安全沙箱.在跨域的时候，一定要加上这句，不然在对方网站有安全策略的文件夹也是不行的*/
		private var lc:LoaderContext = new LoaderContext(true);
		private var bitmapData:BitmapData;

		/**
		 * 设置数据
		 * 1:url地址直接载入
		 * 2:null移除位图数据
		 * 3:bitmapdata 设置位图数据
		 * @param _url
		 *
		 */
		public function set source(_url:*):void {
			if (_url is String){ /*是地址,需要动态载入*/
				if (loader == null){
					loader = new Loader();
					loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
					loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
				}
				loader.load(new URLRequest(_url), lc);
				TDebug.trace(_url);
			} else if (_url == null){ /*移除数据*/
				this.bitmap.bitmapData = null;
			} else if (_url is BitmapData){ /*是位图数据*/
				if (this.bitmap == null){
					this.bitmap = new Bitmap(_url);
					this.addChildAt(bitmap, 0);
				} else {
					this.bitmap.bitmapData = _url;
					this.addChildAt(bitmap, 0);
				}
			}
		}

		private function onError(event:IOErrorEvent):void {
			//	AlertWindow.Show('没有找到该文件');
		}

		protected function onComplete(event:Event):void {
			if (bitmapData != null){
				bitmapData.dispose();
				bitmapData = null;
			}
			bitmapData = new BitmapData(event.target.loader.width, event.target.loader.height, true, 0);
			bitmapData.draw(event.target.loader, null, null, null, null, true);
			TDebug.trace('载入图片完成' + bitmapData.width + '-' + bitmapData.height);
			if (this.bitmap != null){
				this.bitmap.bitmapData = bitmapData;
				//this.bitmap.width=bitmapWidth;
				//this.bitmap.height=bitmapHeight;
				if (!this._needScale){ /*不需要缩放设置*/
					if (this.w != 0 && this.h != 0){
						this.bitmap.width = w;
						this.bitmap.height = h;
					}
				} else { /*需要缩放设置*/
					if (this.w != 0 && this.h != 0){
						var scaleX:Number = this.w / bitmapData.width; //求出横向缩放大小
						var scaleY:Number = this.h / bitmapData.height; //求出纵向缩放大小
						var smallScale:Number = scaleX > scaleY ? scaleY : scaleX;
						if (smallScale < 1){ /*如果最终的缩放大于正常的大小,就不需要缩放*/
							this.bitmap.scaleX = smallScale;
							this.bitmap.scaleY = smallScale;
						}
					}
				}
			}
			//图片平滑性
//			this.bitmap.smoothing=true;
			if (needDispatch){
				this.dispatchEvent(new ImageDataEvent(bitmapData));
			}
			if (this.corner){
				this.checkCornor(bitmapData);
			}
		}

		private function checkCornor(bitmapData:BitmapData):void {
			//创建矩阵
			var m:Matrix = new Matrix();
			//画出一个box,还可以角度什么之类 都可以设置
			m.createBox(1, 1);
			//用位图图像填充绘图区。可以重复或平铺位图以填充该区域
			this.graphics.beginBitmapFill(bitmapData, m, true, true);
			//绘制一个圆角矩形
			this.graphics.drawRoundRectComplex(0, 0, this.width, this.height, 20, 20, 20, 20);
			this.graphics.endFill();
			this.removeChild(this.bitmap);
		}

		public function TImage(_autoDispose:Boolean = false){
			this.bitmap = new Bitmap();
			this.addChildAt(bitmap, 0);
			if (_autoDispose){
				this.addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
			}
		}

		private function onRemove(event:Event):void {
			this.removeEventListener(Event.REMOVED_FROM_STAGE, onRemove);
			this.dispose();
		}

		override public function dispose():void {
			if (!isDispose){
				super.dispose();
				if (bitmapData != null){
					bitmapData.dispose();
					bitmapData = null;
				}
				this.bitmap = null;
				if (loader != null){ /*有一些就是没有loader进来,采用位图数据*/
					loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
					loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError);
					this.loader.unload();
					this.loader = null;
				}
				lc = null;
			}
		}

		override public function setSize(w:Number, h:Number):void {
			super.setSize(w, h);
			//设置自已的大小
		}

		/**
		 * 设置无损缩份,一直缩分到两个都小于为止
		 * @param w
		 * @param h
		 *
		 */
		public function setScaleSize(w:Number, h:Number):void {
			_needScale = true;
			this.w = w;
			this.h = h;
		}

		/**
		 * 设置图片的大小
		 * @param w 宽度
		 * @param h 高度
		 *
		 */
		public function setImageSize(w:Number, h:Number):void {
			this.w = w;
			this.h = h;
			if (this.bitmap != null){
				this.bitmap.width = w;
				this.bitmap.height = h;
			}
		}
	}
}